package com.gamecollection.snake.controller;

import com.gamecollection.mvc.Controller;
import com.gamecollection.snake.model.SnakeModel;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

/**
 * 贪吃蛇游戏控制器
 */
public class SnakeController implements Controller {
    
    private SnakeModel model;
    
    public SnakeController(SnakeModel model) {
        this.model = model;
    }
    
    @Override
    public void initialize() {
        // 控制器初始化
    }
    
    @Override
    public void handleKeyPressed(KeyEvent e) {
        int state = model.getGameState();
        
        switch (e.getKeyCode()) {
            case KeyEvent.VK_SPACE:
                handleSpaceKey(state);
                break;
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                if (state == SnakeModel.STATE_PLAYING) {
                    model.getHeadObj().changeDirection("up");
                }
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                if (state == SnakeModel.STATE_PLAYING) {
                    model.getHeadObj().changeDirection("down");
                }
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                if (state == SnakeModel.STATE_PLAYING) {
                    model.getHeadObj().changeDirection("left");
                }
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                if (state == SnakeModel.STATE_PLAYING) {
                    model.getHeadObj().changeDirection("right");
                }
                break;
        }
    }
    
    @Override
    public void handleKeyReleased(KeyEvent e) {
        // 键盘释放事件处理
    }
    
    @Override
    public void handleMouseClicked(MouseEvent e) {
        // 鼠标点击事件处理
    }
    
    @Override
    public void handleMouseMoved(MouseEvent e) {
        // 鼠标移动事件处理
    }
    
    @Override
    public void update() {
        // 控制器更新
        int state = model.getGameState();
        
        // 处理游戏状态转换
        if (state == SnakeModel.STATE_RESTART) {
            model.setGameState(SnakeModel.STATE_NOT_STARTED);
            model.reset();
        } else if (state == SnakeModel.STATE_NEXT_LEVEL && model.getLevel() != 3) {
            model.setGameState(SnakeModel.STATE_PLAYING);
            model.nextLevel();
        }
    }
    
    @Override
    public void startGame() {
        model.setGameState(SnakeModel.STATE_PLAYING);
    }
    
    @Override
    public void pauseGame() {
        if (model.getGameState() == SnakeModel.STATE_PLAYING) {
            model.setGameState(SnakeModel.STATE_PAUSED);
        }
    }
    
    @Override
    public void resumeGame() {
        if (model.getGameState() == SnakeModel.STATE_PAUSED) {
            model.setGameState(SnakeModel.STATE_PLAYING);
        }
    }
    
    @Override
    public void endGame() {
        model.setGameState(SnakeModel.STATE_GAME_OVER);
    }
    
    /**
     * 处理空格键
     */
    private void handleSpaceKey(int state) {
        switch (state) {
            case SnakeModel.STATE_NOT_STARTED:
                startGame();
                break;
            case SnakeModel.STATE_PLAYING:
                pauseGame();
                break;
            case SnakeModel.STATE_PAUSED:
                resumeGame();
                break;
            case SnakeModel.STATE_GAME_OVER:
                model.setGameState(SnakeModel.STATE_RESTART);
                break;
            case SnakeModel.STATE_LEVEL_COMPLETE:
                if (model.getLevel() == 3) {
                    // 游戏通关，重新开始
                    model.setGameState(SnakeModel.STATE_RESTART);
                } else {
                    // 下一关
                    model.setGameState(SnakeModel.STATE_NEXT_LEVEL);
                }
                break;
        }
    }
} 